<template>
  <div class="edit-box">
    <el-form :model="form" label-position="top" :rules="formRules" ref="ruleForm">
      <el-form-item label="收款账户" prop="accountId">
        <el-select v-model="form.accountId" placeholder="请选择">
          <el-option
            v-for="item in accountList"
            :key="item.id"
            :label="item.accountName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收款金额" prop="alreadyMoney">
        <el-input v-model.number="form.alreadyMoney"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { findList } from '@/api/account'
import { updateMoney } from '@/api/receive'
export default {
  data() {
    return {
      loading: false,
      form: {
        id: '',
        accountId: '',
        companyId: this.$store.state.user.companyId,
        alreadyMoney: '',
        remark: ''
      },
      accountList: [],
      formRules: {
        accountId: [
          { required: true, message: '请选择收款账户', trigger: 'blur' }
        ],
        alreadyMoney: [
          { required: true, message: '请输入金额', trigger: 'blur' }
        ]
      }
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  methods: {
    initDialogData(obj) {
      this.initAccountList()
      this.form = obj
      this.form.alreadyMoney = this.form.surplusMoney
    },
    initAccountList() {
      findList(this.form.companyId).then(res => {
        this.accountList = res.data
      })
    },
    submit() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          updateMoney(this.form).then(res => {
            if (res.success) {
              this.$emit('reload')
              this.$emit('changeState', false)
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.edit-box {
  padding: 0 50px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
</style>
